﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HomeCare.View;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace HomeCare.Model
{
    class TelaDados
    {
        Conexao con = new Conexao();

        public DataSet ListaTelas()
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Empty;
            query += "select * ";
            query += "from Telas ";

            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sql);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                sql.Close();
                return dataSet;
            }
            catch (Exception ex)
            {
                sql.Close();
                throw new Exception("Erro ao consultar as Telas.");
                //MessageBox.Show(ex.Message);

            }
        }

        public bool InsereTela(Tela tela)
        {

            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("insert into Telas (Tela, Descricao) values ('{0}','{1}')", tela.tela, tela.descricao);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {

                //throw new Exception("Erro ao Inserir Tela.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        public bool AlteraTela(Tela tela)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("update Telas set Tela='{0}', Descricao='{1}' where id='{2}'", tela.tela, tela.descricao, tela.id);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                //throw new Exception("Erro ao Alterar Tela.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        public bool ExcluiTela(Tela tela)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("delete from Telas where Id='{0}'", tela.id);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                //throw new Exception("Erro ao Alterar Tela.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public DataSet ConsultaTela(Tela tela)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = "select * from Telas where (1=1) ";
            query += String.IsNullOrEmpty(tela.descricao) ? String.Empty : (String.Format(" and Descricao like '%{0}%' ", tela.descricao));
            query += String.IsNullOrEmpty(tela.tela) ? String.Empty : (String.Format(" and Tela like '%{0}%' ", tela.tela));
            query += " order by Tela ";
            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sql);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                sql.Close();
                return dataSet;

            }
            catch (Exception ex)
            {
                sql.Close();
                throw new Exception("Erro ao Consultar Tela.");
                //MessageBox.Show(ex.Message);

            }
        }


    }
}
